package com.ioffice.export.excel.template;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.ioffice.exception.IOfficeException;
import com.ioffice.export.excel.SheetUtils;
import com.ioffice.export.excel.sheet.ExcelSheet;
import com.ioffice.export.excel.sheet.SheetGeneric;
import com.ioffice.export.excel.sheet.cell.SheetCell;

public abstract class AbstarctExportExcelTemplate {

	private Workbook excelWorkbook = null;
	private CellStyle cellStyle = null;
	private SheetCell sheetCell=null;
	private Cell cell=null;
	
	public abstract void writeOneExcel(Workbook wb,ExcelSheet sheet)throws IOException,IOfficeException;
	
	public void writeExcel(OutputStream out,ExcelSheet[] sheets)throws IOException,IOfficeException{
		if(excelWorkbook==null)
			excelWorkbook=new HSSFWorkbook();
		setCellStyle(excelWorkbook);
		for(ExcelSheet sheet:sheets){
			writeOneExcel(excelWorkbook, sheet);
		}
		excelWorkbook.write(out);
		excelWorkbook=null;
	}
	
	
	/**
	 * 设置单个Sheet中的待处理的Cell单元格
	 * @param sheet
	 * @param rowCount
	 * @param colCount
	 * @param defaultCellValue 默认填充字符  默认为 “--”
	 */
	public void setSheetCellByRowCountAndColCount(Sheet sheet,int rowCount,int colCount,String defaultCellValue){
		Row row = null;
		Cell cell = null;
		for(int rowIndex=0;rowIndex<rowCount;rowIndex++){
			row=sheet.createRow(rowIndex);
			for(int colIndex=0;colIndex<colCount;colIndex++){
				cell = row.createCell(colIndex);
				cell.setCellStyle(cellStyle);
				cell.setCellValue(defaultCellValue==null?"--":defaultCellValue.trim().length()>0?defaultCellValue.trim():"--");
				cell=null;
			}
		}
	}
	
	
	
	/**
	 * 设置SheetTree各个节点只干对应的信息
	 * @param sheet POI sheet对象
	 * @param title 自定义标题对象
	 */
	protected void setSheetGeneric(Sheet sheet,SheetGeneric generic)throws IOfficeException {
		for(int i=0,size=generic.getChildSize();i<size;i++){
			sheetCell=generic.getSheetCell(i);
			setCellMergedRegion(sheet, sheetCell);
			sheetCell=null;
		}
	}
	
	
	/**
	 * 设置相关Cell对象的跨行和跨列、cell的文本
	 * @param sheet
	 * @param cellTree
	 */
	protected void setCellMergedRegion(Sheet sheet ,SheetCell sheetCell){
		if(sheetCell!=null){
			int x = sheetCell.getX();
			int y = sheetCell.getY();
			int xSpan = sheetCell.getXspan();
			int ySpan = sheetCell.getYspan();
			SheetUtils.mergerSheetCell(sheet, x, y, xSpan, ySpan);
			cell = sheet.getRow(y).getCell(x);
			cell.setCellValue(sheetCell.getCellValue());
			int childCellCount = sheetCell.getChildSize();
			for(int i=0;i<childCellCount;i++){
				if(sheetCell.getSheetCell(i)!=null)
				setCellMergedRegion(sheet,sheetCell.getSheetCell(i));
			}
		}
		cell=null;
	}
	
	
	
	
	//设Cell中的样式
	protected void setCellStyle(Workbook wb){
		this.cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		
		cellStyle.setTopBorderColor((short)100);
		cellStyle.setLeftBorderColor((short)100);
		cellStyle.setRightBorderColor((short)100);
		cellStyle.setBottomBorderColor((short)100);
	}
}
